/*
Copyright (c) 2012 
Ulf Björklund
http://average-uffe.blogspot.com/
http://twitter.com/codeplanner
http://twitter.com/ulfbjo

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using log4net;
using xConnected.Core.Model;
using xConnected.Core.Interfaces.Service;
namespace xConnected.Controllers
{   
   public partial class MessageCompanyUserController : BaseController<MessageCompanyUser>
    {         
        protected IMessageCompanyUserService MessageCompanyUserService;
        protected IMessageService _messageService;
        protected IMessageFolderService _messageFolderService;
        protected ICompanyProfileService _companyProfileService;
        protected IUserService _userService;

        public MessageCompanyUserController(IMessageCompanyUserService MessageCompanyUserService, IMessageService messageService, IMessageFolderService messageFolderService, ICompanyProfileService companyProfileService, IUserService userService)
		{		    
            base.Service = this.MessageCompanyUserService = MessageCompanyUserService;
		    _messageService = messageService;
            _messageFolderService = messageFolderService;
		    _companyProfileService = companyProfileService;
		    _userService = userService;
		}

        [HttpPost]
        public JsonResult DeleteMesCompanyUser(int messageCompanyUserId)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var messageCompanyUser = MessageCompanyUserService.GetById(messageCompanyUserId);
                var userId = _userService.GetUserByUsername(User.Identity.Name).Id;
                //delete message company user if its company profile belongs to current user 
                var deleteMesCompanyUser =
                    _companyProfileService.GetById(messageCompanyUser.CompanyProfileId).UserId == userId;
                var message = messageCompanyUser.Message;
                //delete message if this message company user is the last one left
                var deleteMessage = message.MessageExpertUsers.Count == 0 && message.MessageCompanyUsers.Count == 1 &&
                                    message.MessagePositionUsers.Count == 0;
                if (deleteMesCompanyUser)
                {
                    MessageCompanyUserService.Delete(messageCompanyUser);
                }

                if (deleteMessage)
                {
                    _messageService.Delete(message);
                }

                return Json("OK", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult MoveToTrash(int mesCompanyUserId)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var userId = _userService.GetUserByUsername(User.Identity.Name).Id;
                var messageCompanyUser = MessageCompanyUserService.GetById(mesCompanyUserId);
                messageCompanyUser.MessageFolder =
                    _messageFolderService.GetAll().First(m => m.OwnerId == userId && m.FolderType == 2);
                MessageCompanyUserService.SaveOrUpdate(messageCompanyUser);

                return Json("OK", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}